package com.example.campusforumapi.service.impl;

import com.example.campusforumapi.dao.UserRoleMapper;
import com.example.campusforumapi.entity.Role;
import com.example.campusforumapi.entity.User;
import com.example.campusforumapi.entity.UserRole;
import com.example.campusforumapi.service.UserRoleService;
import com.example.campusforumapi.util.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Role> getRoles() {
        return userRoleMapper.getRoles();
    }

    @Override
    public PageResult getUserRolePage(Integer page, Integer pageSize, Integer roleId) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
//        2.执行查询
        List<Role> roleList = userRoleMapper.list(roleId);
        Page<Role> p = (Page<Role>) roleList;
//        3.封装PageResult对象
        PageResult pageResult = new PageResult(p.getResult(),p.getTotal(),p.getPageSize(),p.getPageNum());
        return pageResult;
    }

    @Override
    public int delUserRole(List<Integer> userIds, List<Integer> roleIds) {
        return userRoleMapper.delUserRole(userIds,roleIds);
    }

    @Override
    public int insertUserRole(UserRole userRole) {
        userRole.setEffectiveTime(LocalDateTime.now());
        return userRoleMapper.insertUserRole(userRole);
    }

    @Override
    public int findsameUserId(Integer userId) {
        return userRoleMapper.findsameUserId(userId);
    }

    @Override
    public int updUserRole(UserRole userRole) {
        userRole.setUpdateTime(LocalDateTime.now());//更新修改时间
        return userRoleMapper.updUserRole(userRole);
    }
}
